package com.youlai.boot.modules.brand.service;

import com.youlai.boot.modules.brand.model.BrandPageQuery;
import com.youlai.boot.modules.brand.model.BrandVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.brand.mapper.BrandMapper;
import com.youlai.boot.modules.brand.model.Brand;
import com.youlai.boot.modules.brand.model.BrandForm;
import com.youlai.boot.modules.brand.model.BrandConverter;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.modules.merchant.model.MerchantVO;
import com.youlai.boot.modules.merchant.model.MerchantPageQuery;
import com.youlai.boot.modules.merchant.mapper.MerchantMapper;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BrandService extends ServiceImpl<BrandMapper, Brand> {

    private final BrandMapper brandMapper;

    private final BrandConverter brandConverter;
    private final MerchantMapper merchantMapper;

    public IPage<BrandVO> getBrandPage(BrandPageQuery query) {
        Page<BrandVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return brandMapper.getBrandPage(page, query);
    }

    public BrandVO getBrandById(Long id) {
        Brand brand = brandMapper.selectById(id);
        return brandConverter.entityToVo(brand);
    }

    public BrandForm getBrandFormById(Long id) {
        Brand brand = brandMapper.selectById(id);
        return brandConverter.entityToForm(brand);
    }

    public Boolean save(BrandForm form) {
        Brand brand = brandConverter.formToEntity(form);
        return brandMapper.insert(brand) > 0;
    }

    public Boolean update(Long id, BrandForm form) {
        Brand brand = brandConverter.formToEntity(form);
        brand.setId(id);
        return brandMapper.updateById(brand) > 0;
    }

    public Boolean delete(Long id) {
        return brandMapper.deleteById(id) > 0;
    }

    /**
     * 通过商户ID查询关联的品牌列表
     * 
     * @param query      分页查询参数
     * @return 品牌分页数据
     */
    public IPage<BrandVO> getBrandsByMerchantId(BrandPageQuery query) {
        Page<BrandVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return brandMapper.getBrandPageByMerchantId(page, query);
    }
}
